#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
class Solution {
public:
    int splitArray(std::vector<int>& nums, int k)
    {
        int n = nums.size();
        int sum = 0;
        for (const int t :nums)
        {
            sum += t;
        }
        int l = 0;
        int r = sum;
        int ans=0;
        while (l <= r)
        {
            int mid = l + ((r-l) >> 1);
            if (cal(nums,mid)<=k)
            {
                ans = mid;
                r = mid - 1;
            }
            else
            {
                l =mid + 1;
            }
        }
        return ans;
        
    }
    int cal(std::vector<int>& nums,int speed)
    {
        int res = 0;
        int sum = 0;
        for (const int t :nums)
        {
            if (t > speed)
            {
                return 51;
            }
            if (sum+t <= speed)
            {
                sum += t;
            }else
            {
                sum=t;
                res++;
            }
        }
        return res+1;
    }
};
